package com.rf.richfitwheel.workflow.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.common.utils.PageParameter;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;
import com.rf.richfitwheel.workflow.dao.PortalTodoSendHisMapper;
import com.rf.richfitwheel.workflow.model.PortalTodoSendHis;
import com.rf.richfitwheel.workflow.model.PortalTodoSendHisExample;
import com.rf.richfitwheel.workflow.service.PortalTodoSendHisService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author hushouquan
 */
@Service
public class PortalTodoSendHisServiceImpl implements PortalTodoSendHisService{
    @Autowired
    private PortalTodoSendHisMapper portalTodoSendHisMapper;
    /**
     * 保存待办发送记录
     *
     * @param appId        appId
     * @param accessKey    accessKey
     * @param secretKey    secretKey
     * @param moduleType   模块
     * @param businessType businessType
     * @param paramJson    参数json
     * @param httpMethod   httpMethod
     * @param success      是否发送成功
     * @param sendCount    发送次数
     * @param apiUrl       接口
     */
    @Override
    public void savePortalTodoSendHis(String appId, String accessKey, String secretKey, String moduleType,
                                      String businessType, String paramJson, String httpMethod, String success,
                                      Integer sendCount, String apiUrl){
        Date nowDate = new Date();
        PortalTodoSendHis portalTodoSendHis = new PortalTodoSendHis();
        portalTodoSendHis.setId(UuidUtil.get32UUID());
        portalTodoSendHis.setAppId(appId);
        portalTodoSendHis.setAccessKey(accessKey);
        portalTodoSendHis.setSecretKey(secretKey);
        portalTodoSendHis.setModuleType(moduleType);
        portalTodoSendHis.setBusinessType(businessType);
        portalTodoSendHis.setParamJson(paramJson);
        portalTodoSendHis.setHttpMethod(httpMethod);
        portalTodoSendHis.setSuccess(success);
        portalTodoSendHis.setSendCount(sendCount);
        portalTodoSendHis.setApiUrl(apiUrl);
        portalTodoSendHis.setCreateTime(nowDate);
        portalTodoSendHis.setLastTime(nowDate);
        portalTodoSendHisMapper.insert(portalTodoSendHis);
    }
    
    /**
     * 修改待办发送记录
     *
     * @param portalTodoSendHis 待办发送记录
     */
    @Override
    public void updatePortalTodoSendHis(PortalTodoSendHis portalTodoSendHis){
        portalTodoSendHisMapper.updateByPrimaryKeySelective(portalTodoSendHis);
    }
    
    /**
     * 根据id查询待办发送记录
     *
     * @param id 主键
     * @return PortalTodoSendHis
     */
    @Override
    public PortalTodoSendHis getPortalTodoSendHisById(String id){
        return portalTodoSendHisMapper.selectByPrimaryKey(id);
    }
    
    /**
     * 分页查询
     *
     * @param params 参数
     * @return 查询结果
     */
    @Override
    public PageUtils list(Map<String, Object> params){
        PageUtils pageUtils=PageUtils.getPageInfo(params);
        Page<PortalTodoSendHis> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        PortalTodoSendHis portalTodoSendHis = (PortalTodoSendHis) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")), PortalTodoSendHis.class);
        PortalTodoSendHisExample example = new PortalTodoSendHisExample();
        PortalTodoSendHisExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(portalTodoSendHis.getAppId())){
            criteria.andAppIdLike("%" + portalTodoSendHis.getAppId() + "%");
        }
        if(StringUtils.isNotBlank(portalTodoSendHis.getSuccess())){
            criteria.andSuccessEqualTo(portalTodoSendHis.getSuccess());
        }
        example.setOrderByClause(" success desc, last_time desc");
        portalTodoSendHisMapper.selectByExample(example);
        pageUtils =new PageUtils(p);
        return pageUtils;
    }
    
    /**
     * 分页查询
     *
     * @param pageParameter 参数
     * @return 查询结果
     */
    @Override
    public PageUtils queryPagePortalTodoSendHis(PageParameter<PortalTodoSendHis> pageParameter){
        PageUtils pageUtils = PageUtils.getPageInfo(pageParameter);
        Page<PortalTodoSendHis> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        PortalTodoSendHis portalTodoSendHis = pageParameter.getDataForm();
        PortalTodoSendHisExample example = new PortalTodoSendHisExample();
        PortalTodoSendHisExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(portalTodoSendHis.getAppId())){
            criteria.andAppIdLike("%" + portalTodoSendHis.getAppId() + "%");
        }
        if(StringUtils.isNotBlank(portalTodoSendHis.getSuccess())){
            criteria.andSuccessEqualTo(portalTodoSendHis.getSuccess());
        }
        if(StringUtils.isNotBlank(portalTodoSendHis.getModuleType())){
            criteria.andModuleTypeEqualTo(portalTodoSendHis.getModuleType());
        }
        example.setOrderByClause(" success, last_time desc");
        portalTodoSendHisMapper.selectByExampleWithBLOBs(example);
        pageUtils =new PageUtils(p);
        return pageUtils;
    }
}
